//
//  AccountQuestionCell.m
//  ImGuider
//
//  Created by llt on 2017/8/24.
//  Copyright © 2017年 AudioCtrip. All rights reserved.
//

#import "AccountQuestionCell.h"
#import "NSDate+Additions.h"
#import "ShowPhotoView.h"

@interface AccountQuestionCell ()

@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UILabel *lineNameLabel;
@property (weak, nonatomic) IBOutlet UILabel *timeLabel;

@property (weak, nonatomic) IBOutlet UIImageView *guiderHeadImageView;
@property (weak, nonatomic) IBOutlet UIImageView *approveTagImageView;

@property (weak, nonatomic) IBOutlet UIImageView *listenTagImageView;
@property (weak, nonatomic) IBOutlet UILabel *replyLengthLabel;

@property (weak, nonatomic) IBOutlet UIView *replyView;
@property (weak, nonatomic) IBOutlet UIImageView *speechImageView;

@property (weak, nonatomic) IBOutlet UILabel *watingReplyView;
@property (weak, nonatomic) IBOutlet ShowPhotoView *photoView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *photoHeight;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *photoTop;

@end

@implementation AccountQuestionCell

- (void)awakeFromNib {
    [super awakeFromNib];
    
    [self.replyView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(replyTapAction)]];
    
    NSMutableArray *imageArray = [NSMutableArray array];
    
    for (int i = 1; i <= 3; i ++) {
        
        UIImage *image = [UIImage imageNamed:[@"speech_" stringByAppendingString:@(i).stringValue]];
        
        [imageArray addObject:image];
    }
    
    self.speechImageView.animationImages = imageArray;
    self.speechImageView.animationDuration = 1.0;
    self.speechImageView.animationRepeatCount = 0;
    
    self.guiderHeadImageView.layer.borderWidth = 0.5f;
    self.guiderHeadImageView.layer.borderColor = kContentColor.CGColor;
    
    self.lineNameLabel.userInteractionEnabled = YES;
    [self.lineNameLabel addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(lineGuestuerAction)]];
}

- (void)setQuestion:(Question *)question {
//    1禁用，2未回复，3已回复，4拒绝
    _question = question;
    
    self.titleLabel.text = question.question;
    self.lineNameLabel.text = [NSString stringWithFormat:@"#%@#",question.title];
    
    [self.guiderHeadImageView setImageURL:[NSURL URLWithString:question.guideheadimg] placeholderImage:[UIImage imageNamed:@"default"]];
    self.approveTagImageView.hidden = ([question.guidevip integerValue] != 1);
    
    if (question.pictureArray.count > 0) {
        
        self.photoHeight.constant = 76;
        self.photoTop.constant = 5;
        self.photoView.dataSource = question.pictureArray;
        self.photoView.superVC = self.superVC;
        self.photoView.hidden = NO;
        
    } else {
        self.photoHeight.constant = 0;
        self.photoTop.constant = 0;
        self.photoView.hidden = YES;
    }
    
    if (question.status.integerValue == 3 && question.atype.integerValue == 2) {
        
        self.replyView.hidden = NO;
        self.watingReplyView.hidden = YES;
        self.listenTagImageView.hidden = question.isread.boolValue;
        
        NSString *dateStr = question.modifytime ? question.modifytime : question.createtime;
        
        NSInteger length = 10;
        
        if (dateStr.length < 10) {
            length = dateStr.length;
        }
        
        self.timeLabel.text = [dateStr substringWithRange:NSMakeRange(0, length)];
        self.replyLengthLabel.text = [NSString stringWithFormat:@"%@s",question.audiotime];
        
        
    } else {
        
        self.replyView.hidden = YES;
        self.watingReplyView.hidden = NO;
        self.listenTagImageView.hidden = YES;
        
        
        NSString *dateStr = question.modifytime ? question.modifytime : question.createtime;
        
        NSInteger length = 10;
        
        if (dateStr.length < 10) {
            length = dateStr.length;
        }
        
        self.timeLabel.text = [dateStr substringWithRange:NSMakeRange(0, length)];
        
        if (question.status.integerValue == 4) {
            self.watingReplyView.text = MYLocalizedString(@"MYQUESTION_REFUSE_DESCRIBE", nil);
            self.watingReplyView.textColor = kThemeRedColor;
        }
        
        if (question.status.integerValue == 2) {
            self.watingReplyView.text = MYLocalizedString(@"MYQUESTION_WAIT_REPLY", nil);
            self.watingReplyView.textColor = kContentColor;
        }
        
    }
}

- (void)startPlay {
    
    [self.speechImageView startAnimating];
}

- (void)stopPlay {
    
    [self.speechImageView stopAnimating];
}

- (void)lineGuestuerAction {
    
    [_delegate lineNameTapped:self question:self.question];
    
}

- (void)replyTapAction {
    
//    self.question.isread = YES;
//    self.listenTagImageView.hidden = YES;
//    [self.speechImageView startAnimating];
//    
//    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//        
//        [self.speechImageView stopAnimating];
//    });
    
    [_delegate answerAudioTapped:self question:self.question];
}

@end
